<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin\GoodsAttr;

class GoodsAttrController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = GoodsAttr::with('specs')->orderBy('id','desc')->paginate($this->pagesize);
//        dd($data);
        return view('admin.admin_goods_attrs.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.admin_goods_attrs.add');

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request,GoodsAttr $goods_attr_model)
    {
//        dd($request->all());
        $this->validate($request,[
            //验证后期补充
        ]);
//        dd($request->except('_token','list'));
        $model = GoodsAttr::create($request->except('_token','list'));
        $spec_list = [];
        foreach (explode(',',$request->input('list')) as $v){
            $spec_list[]=['name'=>$v];
        }
//        dd($spec_list);
        $res = $model->specs()->createMany($spec_list);
        if($res){
            return ['status'=>200,'msg'=>'成功'];
        }
        return ['status'=>-1,'msg'=>'失败'];
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
